package com.ghd.dataviz.controller;

import com.ghd.dataviz.core.constant.ResponseCode;
import com.ghd.dataviz.core.param.DatavizResponse;
import com.ghd.dataviz.service.IFileUploadInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ：ghd
 * @date ：Created in 2020-03-22 20:55
 * @description：
 * @modified By：
 * @version: $
 */
@Slf4j
@Api(tags = "文件上传接口")
@RestController
@RequestMapping("file")
public class FileUploadController {

    private final IFileUploadInfoService fileUploadInfoService;

    public FileUploadController(IFileUploadInfoService fileUploadInfoService) {
        this.fileUploadInfoService = fileUploadInfoService;
    }

    @PostMapping("upload")
    @ApiOperation(value = "文件上传", notes = "上传文件成功后，返回文件id")
    public DatavizResponse<Long> fileUpload(@RequestParam("file") MultipartFile file) {
        if(file.isEmpty()){
            return DatavizResponse.failure(ResponseCode.FILE_NOT_FOUND).message("请选择上传文件").build();
        }
        Long fileId = fileUploadInfoService.insertPageInfo(file);
        return DatavizResponse.success("上传成功！", fileId);
    }
}
